﻿using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using HiauPdm.Bll;
using HiauPdm.Web;

namespace HiauPdm.Web
{
    public partial class WgjCx : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void Pager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            string sWgjWhere = string.Empty;
            if (Request.QueryString["WgjWhere"] != null)
            {
                sWgjWhere = Request.QueryString["WgjWhere"].Trim();
            }
            string sWgjDh = string.Empty;
            if (sWgjWhere.IndexOf("FindByWgjDh|") == 0)
            {
                sWgjDh = sWgjWhere.Substring(12);
                sWgjWhere = string.Empty;
            }
            DataSet ds = null;
            if (sWgjWhere != string.Empty || sWgjDh != string.Empty)
            {
                if (!Page.IsPostBack)
                {
                    DataSet dsCount = BWgjCx.GetWgjCxCount(sWgjWhere, sWgjDh);
                    if (dsCount != null)
                    {
                        Pager1.RecordCount = (int)dsCount.Tables[0].Rows[0][0];
                    }
                    if (Request.QueryString["PageIndex"] != null && BGlobal.IsNumeric(Request.QueryString["PageIndex"]))
                    {
                        Pager1.CurrentPageIndex = Convert.ToInt32(Request.QueryString["PageIndex"]);
                    }
                }
                ds = BWgjCx.GetWgjCx(sWgjWhere, sWgjDh, Pager1.PageSize, Pager1.CurrentPageIndex);
            }
            if ((ds == null) || (ds.Tables[0].Rows.Count <= 0))
            {
                PanelList.InnerHtml = "<table cellspacing='0' cellpadding='0' border='0' style='border-style:none;border-collapse:collapse;width:832px;text-align:center;'><tr><td>[未找到外购件记录]</td></tr></table>";
            }
            else
            {
                GridWgj.DataSource = ds;
                GridWgj.DataBind();
                SetCzQx();
                GridWgj.SelectedIndex = -1;
                string sWgjId = Request.QueryString["WgjId"];
                if ((sWgjId != null) && (sWgjId.Trim() != string.Empty))
                {
                    for (int iIndex = 0; iIndex < GridWgj.DataKeys.Count; iIndex++)
                    {
                        if (GridWgj.DataKeys[iIndex].Value.ToString() == sWgjId.Trim())
                        {
                            GridWgj.SelectedIndex = iIndex;
                            break;
                        }
                    }
                }
            }
        }

        private void SetCzQx()
        {
            if (!IsAccess("CpSjCx.aspx"))
            {
                for (int i = 0; i < GridWgj.DataKeys.Count; i++)
                {
                    ((HyperLink)GridWgj.Rows[i].Cells[7].Controls[1]).NavigateUrl = string.Empty;
                    ((HyperLink)GridWgj.Rows[i].Cells[7].Controls[1]).ToolTip = string.Empty;
                    ((HyperLink)GridWgj.Rows[i].Cells[7].Controls[1]).ForeColor = Color.Black;
                    ((HyperLink)GridWgj.Rows[i].Cells[7].Controls[1]).Style["cursor"] = "default";
                }
            }
        }

        protected void BtnExport_Click(object sender, EventArgs e)
        {
            string sWgjWhere = string.Empty;
            if (Request.QueryString["WgjWhere"] != null)
            {
                sWgjWhere = Request.QueryString["WgjWhere"].Trim();
            }
            string sWgjDh = string.Empty;
            if (sWgjWhere.IndexOf("FindByWgjDh|") == 0)
            {
                sWgjDh = sWgjWhere.Substring(12);
                sWgjWhere = string.Empty;
            }
            if (sWgjWhere == string.Empty && sWgjDh == string.Empty) { return; }
            BWgjCx.ExportWgjMl(this.Page, BWgjCx.GetWgjCx(sWgjWhere, sWgjDh, int.MaxValue, 1));
        }
    }
}